# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.conf import settings
import pymongo


class JdspiderPipeline(object):
    def __init__(self):
        # 从配置文件中读取信息
        host = settings['MONGODB_HOST']
        port = settings['MONGODB_PORT']
        db_name = settings['MONGODB_DBNAME']

        # 创建数据库链接
        client = pymongo.MongoClient(host=host, port=port)
        # 指定数据库名
        self.mdb = client[db_name]

    def process_item(self, item, spider):
        db = self.mdb[item['big_class']]
        db.insert(dict(item))
        return item
